<template>
  <el-dialog v-model="dialogFormVisible" :title="title" width="800">
    <el-form :model="form" label-width="auto" style="max-width: 800px" :rules="rules">
      <div class="form-scroll">
        <el-row :gutter="20">
          <template v-for="(item, index) in Object.keys(form)">
            <template v-if="item !== 'list' && form[item].type !== 'textarea'">
              <el-col :span="12">
                <el-form-item :label="item">
                  <template v-if="form[item].type === 'datetime'">
                    <el-date-picker
                      v-model="form[item].value"
                      type="date"
                      :placeholder="`请选择${item}`"
                      value-format="YYYY-MM-DD"
                    />
                  </template>
                  <template v-else-if="form[item].type === 'select'">
                    <template v-if="item === '执行人'">
                      <el-select v-model="form[item].value" placeholder="请选择执行人" filterable>
                        <el-option label="王建国" value="wangjianguo"></el-option>
                        <el-option label="李淑华" value="lishuhua"></el-option>
                        <el-option label="张伟" value="zhangwei"></el-option>
                        <el-option label="刘芳" value="liufang"></el-option>
                        <el-option label="陈晨" value="chenchen"></el-option>
                        <el-option label="赵梦琪" value="zhaomengqi"></el-option>
                      </el-select>
                    </template>
                  </template>
                  <template v-else>
                    <el-input v-model="form[item].value" :placeholder="`请填写${item}`" />
                  </template>
                </el-form-item>
              </el-col>
            </template>
            <template v-else>
              <template v-if="form[item].type === 'textarea'">
                <el-col :span="24">
                  <el-form-item :label="item">
                    <el-input type="textarea" v-model="form[item].value" :placeholder="`请填写${item}`" />
                  </el-form-item>
                </el-col>
              </template>
            </template>
          </template>
        </el-row>
      </div>
    </el-form>
    <div>
      <el-button type="primary" @click="onSubmit">{{ isEdit? '编辑': '新增' }}</el-button>
      <el-button @click="dialogFormVisible = false">取消</el-button>
    </div>
  </el-dialog>
</template>

<script setup>
// 方法
import { ref, defineExpose } from 'vue'
const instance = getCurrentInstance();
const { proxy } = instance || {};

// 显示表单
const dialogFormVisible = ref(false)
const title = ref('新增任务')
const isEdit = ref(false)
// 表单
const form = ref({
  '执行人': { type: 'select', value:  '' },
})
// 验证
const rules = ref({
  '执行人': [ { required: true, message: '请选择执行人', trigger: 'blur' }, ],
})

// 打开方法
const open = () => {
  for(let i in form.value) {
    form.value[i].value = ''
  }
  dialogFormVisible.value = true
  isEdit.value = false
  title.value = '发布任务'
}
// 编辑
const edit = (data) => {
  for(let i in form.value) {
    form.value[i].value = data[i]
  }
  isEdit.value = true
  dialogFormVisible.value = true
  title.value = '编辑任务'
}
// 关闭方法
const close = () => {
  dialogFormVisible.value = false
}

// 提交
const onSubmit = () => {
  for(let i in form.value) {
    if(!form.value[i].value) {
      if(form.value[i].type === 'select' || form.value[i].type === 'datetime') {
        proxy.$modal.msgWarning('请选择'+i);
      }else {
        proxy.$modal.msgWarning('请t填写'+i);
      }
      return
    }
  }
  proxy.$modal.msgSuccess('发布成功');
  close()
}

defineExpose({
  open, close, edit
})

</script>

<style scoped lang="scss">

</style>
